<template>
  <div class="my-resource" name="myresource">
    <div class="title pl10">
      <span>热门资源</span>
      <div class="right-icon" @click="lookAll">
        <span class="all">查看全部</span>
        <el-icon class="all-size"><ArrowRightBold /></el-icon>
      </div>
    </div>
    <ul class="resource-list" v-if="list.length">
      <li class="pl10 pr10" v-for="(item, key) in list" :key="key">
        <span>{{ item.originalFileName }}</span>
        <span>
          <el-button link type="primary" @click="downloadFileFn(item)">下载</el-button>
        </span>
      </li>
    </ul>
    <div class="resource-list" v-else>
      <ElEmpty />
    </div>
  </div>
</template>
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import { databaseSort, downloadDatabase, downloadFile } from "@/api/modules/sys";
import { downFiles } from "@/utils/index";

const router = useRouter();
const lookAll = () => {
  router.push({ name: "fileManage" });
};
const list = ref([]);
const getTableList = async () => {
  const newParams = {
    currentPage: 1,
    pageSize: 12
  };
  const { data } = await databaseSort(newParams);
  list.value = data.records;
};
const downloadFileFn = async (params: any) => {
  if (params.customPath) {
    await downloadDatabase({ name: params.name });
    window.open(params.customPath);
  } else {
    downFiles(await downloadFile(params.path), params.originalFileName, async () => {
      await downloadDatabase({ name: params.name });
    });
  }
};
onMounted(() => {
  getTableList();
});
</script>
<style scoped lang="scss">
ul,
li {
  padding: 0;
  margin: 0;
  list-style: none;
}
.my-resource {
  padding: 0 20px 10px;
  background: #ffffff;
  box-sizing: border-box;
  border-radius: 4px;
  height: 100%;
  .title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 72px;
    line-height: 72px;
    font-weight: 600;
    font-size: 16px;
    color: #333333;
    .right-icon {
      display: flex;
      gap: 10px;
      align-items: center;
      font-size: 16px;
      cursor: pointer;
      .el-icon {
        font-size: 12px;
      }
      .all {
        font-weight: 400;
        color: #999999;
        font-size: 14px;
      }
      .all-size {
        font-weight: 400;
        color: #999999;
        font-size: 12px;
      }
    }
  }
  .resource-list {
    // height: 320px;
    li {
      box-sizing: border-box;
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 64px;
      font-size: 14px;
      color: #333333;
      border-bottom: 1px solid #f2f2f2;
      &:hover {
        background: var(--el-color-primary-light-9);
      }
    }
  }
}
</style>
